/**
* 版权声明：厦门睿商网络科技有限公司 版权所有 违者必究
* 日    期：2020-05-18
*/
package com.rzico.order.entity;

import com.rzico.annotation.IgnoreSwaggerParameter;
import com.rzico.basics.entity.Enterprise;
import com.rzico.basics.entity.Member;
import com.rzico.order.enumx.OrderPaymentStatusEnum;
import com.rzico.order.enumx.OrderShippingStatusEnum;
import com.rzico.order.enumx.OrderStatusEnum;
import lombok.Data;

import java.math.BigDecimal;
import java.util.Collections;
import java.util.Date;
import java.util.List;
import javax.persistence.*;
import io.swagger.annotations.ApiModelProperty;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;

/**
 * <pre>
 *   退货申请单数据库对象
 * </pre>
 * @author Rzico Boot
 * @version 1.0
 */
@Data
@JsonIgnoreProperties(value = {"handler"})
@JsonInclude(JsonInclude.Include.NON_NULL)
@Table(name = "wx_askfor")
public class Askfor{
    @Id
    @ApiModelProperty(value = "" )
    protected Long id;

    @ApiModelProperty(value = "创建日期" )
    protected java.util.Date createDate;

    @ApiModelProperty(value = "修改日期" )
    protected java.util.Date modifyDate;

    @JsonIgnore
    @ApiModelProperty(value = "是否删除" ,hidden = true )
    protected Boolean deleted;

    @ApiModelProperty(value = "申请单号" )
    protected String sn;

    @ApiModelProperty(value = "订单类型(0.实时单,1.预约单)")
    protected Integer orderType;

    @ApiModelProperty(value = "所在地" )
    protected Integer areaId;

    @ApiModelProperty(value = "所属企业")
    protected Long companyId;

    @ApiModelProperty(value = "收货地址" )
    protected String address;

    @ApiModelProperty(value = "所在地" )
    protected String areaName;

    @ApiModelProperty(value = "预计送达时间")
    protected java.util.Date estimateDate;

    @ApiModelProperty(value = "收货人" )
    protected String consignee;

    @ApiModelProperty(value = "联系电话" )
    protected String phone;

    @ApiModelProperty(value = "退货说明" )
    protected String memo;

    @ApiModelProperty(value = "退货理由" )
    protected String reason;

    @ApiModelProperty(value = "申请状态(0:未确认,1:已同意,2:已寄回,3.待退款,4.已完成,5.已关闭)" )
    protected Integer status;

    @ApiModelProperty(value = "配送方式(0.普通快递,1.到店核销,2.同城物流)")
    protected Integer shippingMethod;

    @ApiModelProperty(value = "订货方式(0.线上订单，1.人工报单，2.门店销售)")
    protected Integer orderMethod;

    @ApiModelProperty(value = "是否已分配库存", hidden = true)
    protected Boolean isAllocatedStock;

    @ApiModelProperty(value = "厨师ID")
    protected Long chefId;

    @ApiModelProperty(value = "取餐号/桌号")
    protected Integer num;

    @ApiModelProperty(value = "餐具份数")
    protected Integer tableware;

    @ApiModelProperty(value = "能调配厨师")
    protected Boolean allocateChef;

    @ApiModelProperty(value = "包装费" )
    protected java.math.BigDecimal packagingFee;

    @ApiModelProperty(value = "优惠券/红包" )
    protected java.math.BigDecimal couponDiscount;

    @ApiModelProperty(value = "买方会员" )
    protected Long memberId;

    @ApiModelProperty(value = "来源订单" )
    protected Long orderId;

    @ApiModelProperty(value = "所属门店" )
    protected Long shopId;

    @ApiModelProperty(value = "来源订单号")
    protected String orderSn;

    @ApiModelProperty(value = "所属企业" )
    protected Long enterpriseId;

    @ApiModelProperty(value = "退货时间" )
    protected java.util.Date orderDate;

    @ApiModelProperty(value = "到期时间" )
    protected java.util.Date expire;

    @ApiModelProperty(value = "商品合计金额" )
    protected java.math.BigDecimal subPrice;

    @ApiModelProperty(value = "商品合计数量" )
    protected java.math.BigDecimal subQuantity;

    @ApiModelProperty(value = "交易手续费" )
    protected java.math.BigDecimal fee;

    @ApiModelProperty(value = "骑手运费" )
    protected java.math.BigDecimal riderFee;

    @ApiModelProperty(value = "运费(含楼层费)")
    protected java.math.BigDecimal freight;

    @ApiModelProperty(value = "楼层费")
    protected java.math.BigDecimal levelFreight;

    @ApiModelProperty(value = "赠送金币" )
    protected Long point;

    @ApiModelProperty(value = "是否结算")
    protected Boolean isDistribution;

    @ApiModelProperty(value = "结算时间")
    protected java.util.Date distributionDate;

    @ApiModelProperty(value = "结算金额")
    protected java.math.BigDecimal distributionAmount;

    @ApiModelProperty(value = "活动优惠" )
    protected java.math.BigDecimal discount;

    @ApiModelProperty(value = "活动优惠" )
    protected java.math.BigDecimal offsetAmount;

    @ApiModelProperty(value = "应退金额" )
    protected java.math.BigDecimal amountPayable;

    @ApiModelProperty(value = "已退金额" )
    protected java.math.BigDecimal amountPaid;

    @ApiModelProperty(value = "所属部门" )
    protected String officeId;

    @ApiModelProperty(value = "业务员(employee)")
    @Column(name = "operator")
    protected String operatorId;

    @JsonIgnore
    @ApiModelProperty(value = "创建者" ,hidden = true )
    protected String createBy;

    @JsonIgnore
    @ApiModelProperty(value = "更新者" ,hidden = true )
    protected String modifyBy;

    @ApiModelProperty(value = "开票类型:0.不开票,1.增票,2.普票")
    protected Integer taxType;

    /**
     *  会员
     */
    @JsonIgnore
    @IgnoreSwaggerParameter
    protected Member member;


    /**
     *  会员
     */
    @JsonIgnore
    @IgnoreSwaggerParameter
    protected Enterprise enterprise;

    @ApiModelProperty(value = "订单明细")
    protected List<AskforItem> itemList = Collections.emptyList();

    //是否都不需要发货商品
    @JsonIgnore
    public boolean isAllVirtual() {
        for (AskforItem orderItem:getItemList()) {
            if (orderItem.getSubType().equals(0) || orderItem.getSubType().equals(1)) {
                return false;
            }
        }
        return true;
    }

    public String getMemberName() {
        Member member = getMember();
        if (member!=null) {
            return member.getName();
        } else {
            return null;
        }
    }

    public String getMemberMobile() {
        Member member = getMember();
        if (member!=null) {
            return member.getMobile();
        } else {
            return null;
        }
    }

    public BigDecimal calculateSubPrice() {
        BigDecimal amount = BigDecimal.ZERO;
        for (AskforItem askforItem:getItemList()) {
            amount = amount.add(askforItem.getAmount());

        }
        return amount;
    }

    public BigDecimal calculateSubQuantity() {
        BigDecimal amount = BigDecimal.ZERO;
        for (AskforItem askforItem:getItemList()) {
            amount = amount.add(askforItem.getQuantity());

        }
        return amount;
    }

    public BigDecimal calculateSubDiscount() {
        BigDecimal amount = BigDecimal.ZERO;
        for (AskforItem askforItem:getItemList()) {
            amount = amount.add(askforItem.getDiscount());

        }
        return amount;
    }



    public String getMchName() {
        Enterprise enterprise = getEnterprise();
        if (enterprise!=null) {
            return enterprise.getName();
        } else {
            return null;
        }
    }

    public String getMchLogo() {
        Enterprise enterprise = getEnterprise();
        if (enterprise!=null) {
            return enterprise.getLogo();
        } else {
            return null;
        }
    }

    public BigDecimal calculateAmountPayable() {
        BigDecimal freight = null == this.freight?BigDecimal.ZERO : this.freight;
        BigDecimal discount = null == this.discount?BigDecimal.ZERO : this.discount;
        BigDecimal couponDiscount = null == this.couponDiscount?BigDecimal.ZERO : this.couponDiscount;
        BigDecimal offsetAmount = null == this.offsetAmount?BigDecimal.ZERO : this.offsetAmount;
        BigDecimal packagingFee = null == this.packagingFee?BigDecimal.ZERO : this.packagingFee;

        return calculateSubPrice()
               .subtract(discount)
               .add(packagingFee)
               .subtract(offsetAmount)
               .subtract(couponDiscount)
               .add(freight)
               .setScale(2,BigDecimal.ROUND_HALF_DOWN);
    }

    @JsonIgnore
    public String getTitle() {
        for (AskforItem orderItem:getItemList()) {
            return orderItem.getName();
        }
        return null;
    }

    @ApiModelProperty(value = "订单状态")
    public String getStatusDescr() {
        if (getStatus().equals(0) ) {
            return "待确认";
        } else
        if (getStatus().equals(1)) {
            return "已确认";
        } else
        if (getStatus().equals(2)) {
            return "已退货";
        } else
        if (getStatus().equals(3)) {
            return "待退款";
        } else
        if (getStatus().equals(4)) {
            return "已退款";
        } else
        {
            return "已取消";
        }
    }

    public void setStatusDescr(String value) {

    }

    public AskforItem findAskforItem(Long productId) {
        for (AskforItem askforItem:getItemList()) {
            if (askforItem.getProductId().equals(productId)) {
                return askforItem;
            }
        }
        return null;
    }

}
